package processor;

import java.util.Date;

import models.Actor;

import org.apache.commons.lang3.StringUtils;

import prefix.util.CipherUtil;
import prefix.util.JPAUtil;
import service.exception.ActorException;
import service.exception.ActorException.CODE;
import service.impl.ActorServiceImpl;

public class ActorProcessor extends ActorServiceImpl {

    public ActorProcessor(Actor actor) {
        super(actor);
    }

    public void createActor(String email, String psw, String cellPhone, Actor.TYPE type) throws ActorException {
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(psw))
            throw new ActorException(CODE.INVALIDE_PARAMETER);
        try {
            JPAUtil.beginTransaction();
            this.dbActor.setEmail(email);
            this.dbActor.setCredential(CipherUtil.generatePassword(psw));
            if (!StringUtils.isEmpty(cellPhone)) {
                this.dbActor.setMobile(cellPhone);
            }
            this.dbActor.setType(type);
            this.dbActor.setCreateD(new Date());
            JPAUtil.em().persist(this.dbActor);
            JPAUtil.commitTransaction();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new ActorException(CODE.UNKNOWN, e);
        } finally {
            JPAUtil.closeEM();
        }
    }

    public void updatePersonalInfo(String userName, String idNum, Actor.MaritalStatus maritalStatus,
            Actor.Honourific gender, Actor.IdentityType idType, Date dob, Actor.HousingStatus houseStatus,
            Actor.EducationLevel educationLevel, double mthIncome, String name) throws ActorException {
        try {
            JPAUtil.beginTransaction();
            this.dbActor.setPesonalInfo(userName, idNum, maritalStatus, gender, idType, dob, houseStatus,
                    educationLevel, mthIncome, name);
            this.dbActor.setUpdateD(new Date());
            JPAUtil.em().merge(this.dbActor);
            JPAUtil.commitTransaction();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new ActorException(CODE.UNKNOWN, e);
        } finally {
            JPAUtil.closeEM();
        }
    }
}
